#include "lib.h"
#include <unistd.h>
#include <mmu.h>
#include <env.h>
#include <trap.h>


void syscall_putchar(char ch)
{
	msyscall(SYS_putchar, (int)ch, 0, 0, 0, 0);
}


u_int64_t
syscall_getenvid(void)
{
        return msyscall(SYS_getenvid, 0, 0, 0, 0, 0);
}

void
syscall_yield(void)
{
	msyscall(SYS_yield, 0, 0, 0, 0, 0);
	// For testing
	//msyscall(SYS_putchar, (int)'j', 0, 0, 0, 0);
}


u_int64_t
syscall_env_destroy(u_int64_t envid)
{
	return msyscall(SYS_env_destroy, envid, 0, 0, 0, 0);
}

u_int64_t
syscall_set_pgfault_handler(u_int64_t envid, void (*func)(void), u_int64_t xstacktop)
{
	return msyscall(SYS_set_pgfault_handler, envid, (u_int64_t)func, xstacktop, 0, 0);
}

u_int64_t
syscall_mem_alloc(u_int64_t envid, u_int64_t va, u_int64_t perm)
{
	return msyscall(SYS_mem_alloc, envid, va, perm, 0, 0);
}

u_int64_t
syscall_mem_map(u_int64_t srcid, u_int64_t srcva, u_int64_t dstid, u_int64_t dstva, u_int64_t perm)
{
	return msyscall(SYS_mem_map, srcid, srcva, dstid, dstva, perm);
}

u_int64_t
syscall_mem_unmap(u_int64_t envid, u_int64_t va)
{
	return msyscall(SYS_mem_unmap, envid, va, 0, 0, 0);
}

u_int64_t
syscall_set_env_status(u_int64_t envid, u_int64_t status)
{
	return msyscall(SYS_set_env_status, envid, status, 0, 0, 0);
}

u_int64_t
syscall_set_trapframe(u_int64_t envid, struct Trapframe *tf)
{
	return msyscall(SYS_set_trapframe, envid, (u_int64_t)tf, 0, 0, 0);
}

void
syscall_panic(char *msg)
{
	msyscall(SYS_panic, (u_int64_t)msg, 0, 0, 0, 0);
}

u_int64_t
syscall_ipc_can_send(u_int64_t envid, u_int64_t value, u_int64_t srcva, u_int64_t perm)
{
	return msyscall(SYS_ipc_can_send, envid, value, srcva, perm, 0);
}

void
syscall_ipc_recv(u_int64_t dstva)
{
	msyscall(SYS_ipc_recv, dstva, 0, 0, 0, 0);
}

u_int64_t
syscall_cgetc()
{
	return msyscall(SYS_cgetc, 0, 0, 0, 0, 0);
}
